package com.atguigu.service.impl;

import com.atguigu.dao.DictDao;
import com.atguigu.entity.Dict;
import com.atguigu.service.DictService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
@Transactional
public class DictServiceImpl  implements DictService {
    @Autowired
    private DictDao dictDao;

    @Override
    public List<Map<String, Object>> findZnodes(Long id) {
        // 准备一个返回的数据的集合
        List<Map<String, Object>> zNodes =new ArrayList<>();
        /*[{ id:'0331',name:'n3.3.n1',	isParent:true}]*/
        // 通过父id查询当前数据下面的所以有子数据
        List<Dict> dictList =dictDao.findListByParentId(id);
        for (Dict dict : dictList) {
            Map<String,Object> zNode=new HashMap<>();
            zNode.put("id",dict.getId());
            zNode.put("name",dict.getName());
            Integer count = dictDao.countIsParent(dict.getId());
            zNode.put("isParent",count>0?true:false);
            zNodes.add(zNode);
        }
        return zNodes;
    }

    @Override
    public List<Dict> findListByParentId(Long id) {
        return dictDao.findListByParentId(id);
    }

    @Override
    public List<Dict> findListByDictCode(String code) {
        // 先根据code获取当前数据
        Dict dict= dictDao.getByDictCode(code);
        // 把当前数据id作为父id查询所有子节点
        List<Dict> dictList = dictDao.findListByParentId(dict.getId());
        return dictList;
    }

    @Override
    public String getNameById(Long id) {
        return dictDao.getNameById(id);
    }
}
